<template>
    <h2 style="padding-left: 350px;">母猪分娩信息修改</h2>
    <el-form :model="form" label-width="auto" style="max-width: 1000px">
        <br>
        <table>
            <tr>
                <td style="width:400px">
                    <el-form-item label="耳号">
          <el-select v-model="form.basicInforId" placeholder="请选择">
            <el-option v-for="item in BasicInformationList" :label="item.earNumber" :value="item.basicInforId" />
          </el-select>
        </el-form-item>
                </td>
                <td style="width:400px">
                    <el-form-item label="妊娠天数">
                        <el-input v-model="form.gestationNum" style="width:300px" />
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="分娩难易">
                        <el-select v-model="form.difficultieId" placeholder="请选择" clearable>
                            <el-option v-for="item in DifficultiesList" :label="item.difficultieName"
                                :value="item.difficultieId" />
                        </el-select>
                    </el-form-item>
                </td>
                <td>
                    <el-form-item label="肥羊批次">
                        <el-select v-model="form.batchId" placeholder="请选择" clearable>
                            <el-option v-for="item in StudiesList" :label="item.batchName" :value="item.batchId" />
                        </el-select>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="母性">
                        <el-select v-model="form.maternalId" placeholder="请选择" clearable>
                            <el-option v-for="item in MaternalsList" :label="item.maternalName"
                                :value="item.maternalId" />
                        </el-select>
                    </el-form-item>
                </td>
                <td>
                    <el-form-item label="泌乳">
                        <el-input v-model="form.lactation" />
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="乳房炎">
                        <el-input v-model="form.mastitis" />
                    </el-form-item>
                </td>
                <td>
                    <el-form-item label="窝号">
                        <el-input v-model="form.nestNumber" />
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="活猪数">
                        <el-input v-model="form.livePigNum" />
                    </el-form-item>
                </td>
                <td>
                    <el-form-item label="窝总重(公斤)">
                        <el-input v-model="form.nestWeight" />
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="性别">
                        <el-radio-group v-model="form.gender" disabled="true">
                            <el-radio :value="1">男</el-radio>
                            <el-radio :value="2" checked>女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </td>
                <td width="150">
                    <el-form-item label="当前栋舍">
                        <el-input v-model="form.turnBuilding" disabled="true"/>
                    </el-form-item>
                </td>
            </tr>
            <tr>
            <td width="150">
                <el-form-item label="当前栏位">
                    <el-input v-model="form.turnColumn" disabled="true"/>

                    </el-form-item>
            </td>
                <td>
                    <el-form-item label="转入栋舍">
                        <el-select v-model="form.bedroomId" placeholder="请选择" @change="handlefield()" clearable >
                            <el-option v-for="bedroom in bedroomList" :key="bedroom.bedroomId" :label="bedroom.bedroomName" :value="bedroom.bedroomId" />
                        </el-select>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>
                    <el-form-item label="转入栏位">
                        <el-select v-model="form.fieldId" placeholder="请选择" clearable >
                            <el-option v-for="field in fieldList" :key="field.fieldId" :label="field.fieldName" :value="field.fieldId" />
                        </el-select>
                    </el-form-item>
                </td>
                <td>
                </td>
            </tr>
        </table>
        <el-form-item>
            <el-button style="margin-left: 350px;width:100px" type="primary" @click="onSubmit">修改</el-button>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

const router = useRouter();
const route = useRoute();
onMounted(() => {
    GetBasicInformation();//耳号
    GetDifficulties();//生产难易
    GetStudies();//批次
    GetMaternals();//母性
    handleBedroom();//栋舍
    GetFarrowings();//返填
})
//母猪分娩修改信息
const form = reactive({
    farrowingId:'',
    basicInforId: '',
    gestationNum: '',
    difficultieId: '',
    batchId: '',
    maternalId: '',
    lactation: '',
    mastitis: '',
    nestNumber: '',
    livePigNum: '',
    nestWeight: '',
    gender: '',
    turnBuilding: '',
    turnColumn: '',
    bedroomId: '',
    fieldId: '',
    isFarrowings: 0,
})

//母猪分娩信息修改
const onSubmit = () => {
    axios.put(apiBedinUrl+'/api/Farrowings/UpdateFarrowings', form)
        .then(res => {
            if (res.data.code == 0) {
                console.log("母猪分娩修改", res.data);
                ElMessage.success('母猪分娩信息修改成功');
                router.push('/ShowFarrowings');
            }
            else {
                ElMessage.error('母猪分娩信息修改失败');
                return;
            }
        })
}

//返填母猪分娩
const GetFarrowings=()=>{
    axios.post(apiBedinUrl+'/api/Farrowings/GetFarrowings',{
        farrowingId:route.query.farrowingId
    })
    .then(res=>{
        console.log(res.data.data);
        form.farrowingId=res.data.data.farrowingId;
        form.basicInforId=res.data.data.basicInforId;
        form.gestationNum=res.data.data.gestationNum;
        form.difficultieId=res.data.data.difficultieId;
        form.batchId =res.data.data.batchId;
        form.maternalId =res.data.data.maternalId;
        form.lactation =res.data.data.lactation;
        form.mastitis =res.data.data.mastitis;
        form.nestNumber =res.data.data.nestNumber;
        form.livePigNum =res.data.data.livePigNum;
        form.nestWeight =res.data.data.nestWeight;
        form.gender =res.data.data.gender;
        form.bedroomId =res.data.data.bedroomId;
        form.fieldId =res.data.data.fieldName;
        form.turnBuilding =res.data.data.turnBuilding;
        form.turnColumn =res.data.data.turnColumn;
    })
}

//猪只信息耳号列表
const BasicInformationList=ref([]);
const GetBasicInformation=()=>{
    axios.post(apiBedinUrl+'/api/Infancies/GetBasicInformation',{})
    .then(res=>{
        console.log("猪只信息耳号",res.data.data);
        BasicInformationList.value = res.data.data;
    })
}


//分娩难易列表
const DifficultiesList = ref([]);
const GetDifficulties = () => {
    axios.post(apiBedinUrl+'/api/Farrowings/GetDifficulties', {})
        .then(res => {
            console.log("分娩难易列表", res.data.data);
            DifficultiesList.value = res.data.data;
        })
}

//肥猪批次列表
const StudiesList = ref([]);
const GetStudies = () => {
    axios.post(apiBedinUrl+'/api/Farrowings/GetStudies', {})
        .then(res => {
            console.log("肥猪批次列表", res.data.data);
            StudiesList.value = res.data.data;
        })
}

//母性列表
const MaternalsList = ref([]);
const GetMaternals = () => {
    axios.post(apiBedinUrl+'/api/Farrowings/GetMaternals', {})
        .then(res => {
            console.log("母性列表", res.data.data);
            MaternalsList.value = res.data.data;
        })
}

//响应式数据 获取栋舍
const condition=reactive({
    pageIndex:1,
    pageSize:100,
    bedroomTypeId:0,
    bedroomName:'',
    personName:''
})

//获取栋舍
const bedroomList:any=ref([])
const handleBedroom=()=>{
    axios.post(apiUrl+'/api/BedRoom/ShowBedroom',{
        pageIndex: condition.pageIndex,
        pageSize: condition.pageSize,
        bedroomTypeId: condition.bedroomTypeId,
        bedroomName: condition.bedroomName,
        personName:condition.personName
    }).then(res=>{
        bedroomList.value=res.data.data.list
    })
}

//获取栏位
const fieldList:any=ref([])
const handlefield=()=>{
    axios.post(apiUrl+'/api/BedRoom/GetBrdroomIdField',{})
    .then(res=>{
        console.log("栏位",res.data.data);
        fieldList.value=res.data.data
    })
}
</script>
